import requests
from requests_toolbelt import MultipartEncoder
# go版结算接口调用

class User(object):
    def __init__(self, cookie_str):
        self.cookie_str = cookie_str

    # 请求结算前台
    def get_sale_buy(self):
        url = "http://localhost:5002/api/v2/user/sale/buy/14"
        # 添加请求头
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
            'Cookie': self.cookie_str
        }
        res = requests.post(url, headers=headers).text
        return res

    def post_sale_buy(self):  # 服务消费测试
        url = "http://localhost:5002/api/v2/user/sale/buysave/15"
        request_body = MultipartEncoder(
            {
                'Nid': '15',
                'fahuo': '公司自提',
                'num_data': '{"1":2}',
                'set_number': '',
                'pay_type': '常规扣款',
                'set_money': '',
                'setMoney': '23',
                'money': '',
                'device': 'pc',
                'n_id': '',
                'ip': '',
            }
        )
        request_header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
            'Cookie': self.cookie_str,
            "Content-Type": request_body.content_type
        }
        res = requests.post(url, data=request_body, headers=request_header).text
        return res

    def post_sale_up(self):  # 前台升级 9
        url = "http://localhost:5002/api/v2/user/sale/upsave/9"
        request_body = MultipartEncoder(
            {
                'Nid': '9',
                'fahuo': '公司自提',
                'pay_type': '常规扣款',
                'num_data': '{"1":22}',
                # 'setMoney': '200',
                'lv': '2',  # 2 普通经销商
                'money': '',
                'device': 'pc',
                'n_id': '',
                'ip': '',
            }
        )
        request_header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
            'Cookie': self.cookie_str,
            "Content-Type": request_body.content_type
        }
        res = requests.post(url, data=request_body, headers=request_header).text
        return res


if __name__ == '__main__':
    # print(get_sale_buy())
    # 使用前到浏览器登录用户并复制个cookie过来
    u1 = User('beegosessionID=b49e82322dfe9ebed25e03704d1ca15e')
    # print(u1.get_sale_buy())
    print(u1.post_sale_up())
